﻿goog.provide('imc.cmd.GridSolicitudes');

var curSolicitud;

//Tabla Solicitudes --------------------------------------------------------------------------------------------------
imc.cmd.GridSolicitudes.dataTableSolicitudes = function (todas, idStatus, idTipo, idRol) {
    imc.cmd.Loader.show();
    $('#contenedorDerecha').load("solicitudes.html #tablaSolicitudes", function () {
        $.ajax({
            url: "Services/Auth.svc/RecuperaTodasRol",
            type: "POST",
            dataType: "json",
            data: '{"todas": "' + todas + '","idStatus": ' + idStatus + ' ,"idTipo":' + idTipo + ',"unidRol":"' + idRol + '"}',
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                var oTableTblSolicitudes = $('#tblSolicitudes').dataTable({
                    'bInfo': false,
                    'bPaginate': false,
                    'bLengthChange': false,
                    'bRetrieve': true,
                    'sDom': '',
                    'sInfoEmpty': '',
                    aaData: data.RecuperaTodasRolResult,
                    aoColumns: [
                        { "mDataProp": 'prioridad',
                            "bSortable": false,
                            "fnRender": function (obj) {
                                var sIconName = obj.aData['prioridad'];
                                if (sIconName) {
                                    return '<img src="' + sIconName + '" width="15" height="20"/>';
                                } else {
                                    return "";
                                }
                            }, 
                            "bUseRendered": false,
                            "sWidth": "1%"
                        },
                        { "sTitle": "Tipo Solicitud", "mDataProp": "tipoSol" },
		                { "sTitle": "Persona", "mDataProp": "persona" },
		                { "sTitle": "Razón Social", "mDataProp": "razonSocial", "sWidth": "50%" },
                        { "sTitle": "Creación", "mDataProp": "creacion" }
                    ]
                });
                if (oTableTblSolicitudes.fnGetData().length != 0) {
                    $('#tblSolicitudes tbody tr').die();
                    var cuenta = 0;
                    $('#tblSolicitudes tbody tr').live('click', function () {
                        if (cuenta == 0) {
                            cuenta++;
                            h.setToken("Solicitudes/solicitud");
                            var nTds = $('td', this);
                            var data = oTableTblSolicitudes.fnGetData(this);
                            console.log(data);
                            sDataSol = "";
                            for (var i in data) {
                                if (i = 'id') {
                                    sDataSol = data[i];
                                }
                            }
                            console.log(sDataSol);
                            var sDataTipo = $(nTds[1]).text();
                            curSolicitud = new imc.cmd.Solicitud(sDataSol, sDataTipo);
                            return true;
                        }
                        else {
                            alert("Ya ha sido enviada una petición para abrir la solicitud.");
                            return false;
                        }
                    });
                }
                else {
                    return;
                }
            },
            error: function () {
                window.location.href = "paginaDeError.html";
                console.log('Error al cargar el grid de solicitudes');
            }
        });
    });


}

